package com.itheima.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.travel.mapper.FavoriteMapper;
import com.itheima.travel.mapper.RouteMapper;
import com.itheima.travel.mapperExt.FavoriteMapperExt;
import com.itheima.travel.mapperExt.RouteMapperExt;
import com.itheima.travel.pojo.Favorite;
import com.itheima.travel.pojo.FavoriteExample;
import com.itheima.travel.pojo.Route;
import com.itheima.travel.req.FavoriteVo;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.req.UserVo;
import com.itheima.travel.service.FavoriteService;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import com.sun.corba.se.impl.ior.FreezableList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 *
 */
@Service
public class FavoriteServiceImpl implements FavoriteService {
    @Autowired
    private FavoriteMapper favoriteMapper;
    @Autowired
    private FavoriteMapperExt favoriteMapperExt;
    @Autowired
    private RouteMapper routeMapper;
    @Autowired
    private RouteMapperExt routeMapperExt;
    @Autowired
    private HttpSession session;


    @Transactional
    @Override
    public Integer addFavorite(FavoriteVo favoriteVo) {
        // 添加收藏记录
        // 先获得当前用户，如果没有用户登陆的话是不可以收藏的
        Object object = session.getAttribute(session.getId());
        UserVo userVo = null;
        // 进行判断
        if (!EmptyUtil.isNullOrEmpty(object)){
            userVo = (UserVo)object;
        }else {
            return null;
        }
        // 收藏表就获得了userId这个字段的值
        favoriteVo.setUserId(userVo.getId());
        // 调用dao层的方法插入值,参数是favorite的所以要转换
        favoriteMapper.insert(BeanConv.toBean(favoriteVo, Favorite.class));
        // 与此同时，线路表中的count字段应该+1
        Long flag = routeMapperExt.updateRouteCountById(favoriteVo.getRouteId());
        if (flag==0){
            throw new RuntimeException("修改统计表出错");
        }
        //再次查询收藏表
        Route route = routeMapper.selectByPrimaryKey(favoriteVo.getRouteId());
        return route.getAttentionCount();
    }

    @Override
    public PageInfo<RouteVo> findMyFavorite(int pageNum, int pageSize) {
        // 使用分页
        PageHelper.startPage(pageNum,pageSize);
        // 查询我的收藏,需要进行分页查询
        // 需要先进行判断，是否已经登陆，登陆后才可以查，否则不能查询
        Object object = session.getAttribute(session.getId());
        // 登陆后session中存的数据是UserVo类型
        UserVo userVo = null;
        // 进行判断
        if (!EmptyUtil.isNullOrEmpty(object)){
            // 非空则把查询到的数据赋值给userVo
            userVo = (UserVo)object;
        }else {
            return null;
        }
        // 当确定了已经登陆后,需要对收藏表进行分页查询
        List<Route> list = favoriteMapperExt.findFavoriteByUserId(userVo.getId());
        // 返回值是PageInfo<RouteVo>,需要对其中的元素进行拷贝，再返回
        PageInfo<Route> pageInfo = new PageInfo<>(list);
        PageInfo<RouteVo> pageInfoVo = new PageInfo<>();
        // 外部对象拷贝
        BeanConv.toBean(pageInfo,pageInfoVo);
        // 结果集转换,内部拷贝,因为list再PageInfo的父类中,子类拷贝并不能把父类中的参数也拷贝
        List<RouteVo> routeVoList = BeanConv.toBeanList(pageInfo.getList(), RouteVo.class);
        pageInfoVo.setList(routeVoList);
        return pageInfoVo;

    }

    @Override
    public Boolean isFavorited(FavoriteVo favoriteVo) {
        // 获取当前登陆的用户
        Object object = session.getAttribute(session.getId());
        UserVo userVo = null;
        if (!EmptyUtil.isNullOrEmpty(object)){
            userVo = (UserVo)object;
        }else {
            return null;
        }
        // 新建条件
        FavoriteExample favoriteExample = new FavoriteExample();
        favoriteExample.createCriteria()
                .andUserIdEqualTo(userVo.getId())
                .andRouteIdEqualTo(favoriteVo.getRouteId());
        List<Favorite> list = favoriteMapper.selectByExample(favoriteExample);
        // 如果list集合中有数据,说明已经被收藏
        return list.size()>0;
    }
}
